package com.love.mview;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;

/**
 * Created by zf on 2016/4/10.
 */
public class marc extends View {
    private Paint paintBj;
    private Paint paint1Color;
    private RectF rectF;
    private int width;
    private int value = 0;

    /**
     * 园的半径
     */
    private int circleRadius = 0;
    /**
     * 园的y轴起始坐标
     */
    private int circleStartY = 30;
    /**
     * 园的y轴终点坐标 起始坐标加上园的半径*2
     */
    private int circleEndy = 0;

    public marc(Context context) {
        this(context, null);
    }

    public marc(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public marc(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

        width = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        circleRadius = width / 4;

        circleEndy = circleStartY + circleRadius * 2;
        rectF = new RectF(width / 2 - circleRadius, circleStartY, width / 2 + circleRadius, circleEndy);// 弧形
    }


    private void init() {
        paintBj = new Paint();
        paintBj.setAntiAlias(true);
        paintBj.setColor(getResources().getColor(R.color.colorPrimary));
        paintBj.setStyle(Paint.Style.STROKE);
        paintBj.setStrokeCap(Paint.Cap.ROUND);
        paintBj.setStrokeWidth(20);

        paint1Color = new Paint();
        paint1Color.setAntiAlias(true);
        paint1Color.setColor(getResources().getColor(R.color.colorAccent));
        paint1Color.setStyle(Paint.Style.STROKE);
        paint1Color.setStrokeCap(Paint.Cap.ROUND);
        paint1Color.setStrokeWidth(20);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(rectF, 180, 360, false, paintBj);
        canvas.drawArc(rectF, 180, value, false, paint1Color);
    }

    public void setValue(int value){
        this.value = value;
        //playAnimation();
        invalidate();
    }

    private void playAnimation() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "animationSeek", 0.0f, 1.0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(1000);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.setTarget(this);
        animatorSet.play(animator);
        animatorSet.start();
    }

}
